// pages/previewCard/previewCard.js
Page({
  data: {
    value: '喜迎元宵，早送祝愿：心圆梦圆，万事皆圆；人缘爱缘，缘缘美满；财源福源，左右逢源；友缘情缘，永不间断。祝你元宵佳节快乐无边，幸福无限！',
    filePath: 'https://fcdn.qiushilaile.com/adx/app-89171610422656514.jpg'
  },
  onLoad: function (query) {
    console.log(query)
    this.setData({
      value: query.value,
      filePath: query.images,
      top: query.top,
      left: query.left,
      num: query.num,
    });
  },
  onShow: function () {
    // this.util();
    this.getHeight();
  },
  util: function (obj) {
    console.log(obj);
    console.log(-obj.list);
    var continueTime = (parseInt(obj.list / obj.container) + 1) * 5000;
    var setIntervalTime = 50 + continueTime;
    var animation = tt.createAnimation({
      duration: 200,  // 动画时长
      timingFunction: "linear", // 线性
      delay: 0  // 0则不延迟
    });
    this.animation = animation;
    animation.translateY(obj.container)
      .step({ duration: 50, timingFunction: 'step-start' })
      .translateY(-obj.list)
      .step({ duration: continueTime });
    this.setData({
      animationData: animation.export()
    })
    setInterval(() => {
      animation.translateY(obj.container)
        .step({ duration: 50, timingFunction: 'step-start' })
        .translateY(-obj.list)
        .step({ duration: continueTime });
      this.setData({
        animationData: animation.export()
      })
    }, setIntervalTime)
  },
  getHeight() {
    var obj = new Object();
    //创建节点选择器
    var query = tt.createSelectorQuery();
    query.select('.container').boundingClientRect()
    query.select('.list').boundingClientRect()
    query.exec((res) => {
      obj.container = res[0].height; // 框的height
      obj.list = res[1].height; // list的height
      // return obj;
      this.util(obj);
    })
  },
  save: function () {
    console.log('保存图片')
    tt.downloadFile({
      url: this.data.filePath,
      header: {
        "content-type": "application/json",
      },
      success: (res) => {
        let filePath = res.tempFilePath;
        tt.saveImageToPhotosAlbum({
          filePath, // 暂不支持网络图片/本地图片地址，需与tt.downloadFile一起使用
          success: (res) => {
            tt.showToast({ title: "成功保存到本地相册" });
          },
          fail: (err) => {
            let errType = err.errMsg.includes("saveImageToPhotosAlbum:fail cancel")
              ? "取消保存"
              : "保存失败";
            tt.showModal({
              title: errType,
              content: err.errMsg,
              showCancel: false,
            });
          },
        });
      },
      fail: (err) => {
        tt.showModal({
          title: "下载出错",
          content: err.errMsg,
          showCancel: false,
        });
      },
    });
    tt.navigateTo({
      url: '../../pages/index/index' // 指定页面的url
    });
  },
  back() {
    wx.navigateBack({
      delta: 1
    })
  },
})